home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 6 / QRZ Ham Radio Callsign Database - Volume 6.iso / pc / files / dsp / dspkgctr.z / dspkgctr / gcc / stor-layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-08  |  32.5 KB  |  1,119 lines

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4.    $Id: stor-layout.c,v 1.6 91/10/23 17:07:24 pete Exp $
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. #include "config.h"
  24. #include <stdio.h>
  25.  
  26. #include "tree.h"
  27. #include "rtl.h"   /* For GET_MODE_SIZE */
  28. #if defined( DSP96000 ) || defined( DSP56000 )
  29. #include "c-tree.h"
  30. #endif
  31.  
  32. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  33. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  34. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  35.  
  36. /* Data type for the expressions representing sizes of data types.
  37.    It is the first integer type laid out.
  38.    In C, this is int.  */
  39.  
  40. tree sizetype;
  41.  
  42. /* An integer constant with value 0 whose type is sizetype.  */
  43.  
  44. tree size_zero_node;
  45.  
  46. /* An integer constant with value 1 whose type is sizetype.  */
  47.  
  48. tree size_one_node;
  49.  
  50. #define GET_MODE_ALIGNMENT(MODE)   \
  51.   MIN (BIGGEST_ALIGNMENT,        \
  52.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  53.  
  54. /* Chain of all permanent types we have allocated since last
  55.    call to get_permanent_types.  */
  56.  
  57. tree permanent_type_chain;
  58.  
  59. /* Chain of all temporary types we have allocated in this function.  */
  60.  
  61. tree temporary_type_chain;
  62.  
  63. /* When the chains is not null, these point at the last
  64.    types on the two chains.  These help us tell whether a type
  65.    is already on a chain.  */
  66. tree permanent_type_end;
  67. tree temporary_type_end;
  68.  
  69. #if defined( _MSDOS )
  70. void error ( char *, ... );
  71. void error_with_decl ( tree decl, ... );
  72. #endif
  73.  
  74. /* Put the newly-made type T
  75.    on either permanent_type_chain or temporary_type_chain.
  76.    Types that are const or volatile variants of other types
  77.    are not put on any chain, since in the gdb symbol segment
  78.    we do not make those distinctions.
  79.  
  80.    If T is already on the chain, we do nothing.  */
  81.  
  82. void
  83. chain_type (t)
  84.      tree t;
  85. {
  86.   if (TYPE_MAIN_VARIANT (t) != t)
  87.     return;
  88.   if (TREE_CHAIN (t) != 0)
  89.     return;
  90.   if (TREE_PERMANENT (t))
  91.     {
  92.       /* If T is on the chain at the end, don't chain it to itself!  */
  93.       if (t == permanent_type_end)
  94.     return;
  95.       /* Add T to the end of the chain.  */
  96.       if (permanent_type_chain == 0)
  97.     permanent_type_chain = t;
  98.       else
  99.     TREE_CHAIN (permanent_type_end) = t;
  100.       permanent_type_end = t;
  101.     }
  102.   else
  103.     {
  104.       if (t == temporary_type_end)
  105.     return;
  106.       if (temporary_type_chain == 0)
  107.     temporary_type_chain = t;
  108.       else
  109.     TREE_CHAIN (temporary_type_end) = t;
  110.       temporary_type_end = t;
  111.     }
  112. }
  113.  
  114. /* Get a chain of all permanent types made since this function
  115.    was last called.  */
  116.  
  117. tree
  118. get_permanent_types ()
  119. {
  120.   register tree tem = permanent_type_chain;
  121.   permanent_type_chain = 0;
  122.   permanent_type_end = 0;
  123.   return tem;
  124. }
  125.  
  126. /* Get a chain of all temporary types made since this function
  127.    was last called.  */
  128.  
  129. tree
  130. get_temporary_types ()
  131. {
  132.   register tree tem = temporary_type_chain;
  133.   temporary_type_chain = 0;
  134.   temporary_type_end = 0;
  135.   return tem;
  136. }
  137.  
  138. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  139.  
  140. static tree pending_sizes;
  141.  
  142. /* Nonzero means cannot safely call expand_expr now,
  143.    so put variable sizes onto `pending_sizes' instead.  */
  144.  
  145. int immediate_size_expand;
  146.  
  147. tree
  148. get_pending_sizes ()
  149. {
  150.   tree chain = pending_sizes;
  151.   pending_sizes = 0;
  152.   return chain;
  153. }
  154.  
  155. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  156.    to serve as the actual size-expression for a type or decl.  */
  157.  
  158. static tree
  159. variable_size (size)
  160.      tree size;
  161. {
  162.   size = save_expr (size);
  163.  
  164.   if (global_bindings_p ())
  165.     {
  166.       error ("variable-size type declared outside of any function");
  167.       return build_int (1);
  168.     }
  169.  
  170.   if (immediate_size_expand)
  171.     expand_expr (size, 0, VOIDmode, 0);
  172.   else
  173.     pending_sizes = tree_cons (0, size, pending_sizes);
  174.  
  175.   return size;
  176. }
  177.  
  178. /* Return the machine mode to use for an aggregate of SIZE bits.
  179.  
  180.    Note!!!  We only use a non-BLKmode mode if the size matches exactly.
  181.    There used to be the idea of using DImode for anything whose
  182.    size was less than DImode but more than SImode.  This does not work
  183.    because DImode moves cannot be used to store such objects in memory.  */
  184.  
  185. #ifndef MAX_FIXED_MODE_SIZE
  186. #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
  187. #endif
  188.  
  189. static
  190. enum machine_mode
  191. agg_mode (size)
  192.      unsigned int size;
  193. {
  194.   register int units = size / BITS_PER_UNIT;
  195.   register enum machine_mode t, val;
  196.  
  197. #if defined( DSP96000 ) || defined( DSP56000 )
  198.   /* we may want to eventually check for memory_model here ! */
  199.   return BLKmode;
  200. #endif
  201.   if (size % BITS_PER_UNIT != 0)
  202.     return BLKmode;
  203.  
  204.   if (size > MAX_FIXED_MODE_SIZE)
  205.     return BLKmode;
  206.  
  207.   /* Get the last mode which has this size.  */
  208.   val = BLKmode;
  209.   for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  210.        t = (enum machine_mode) ((int) t + 1))
  211.     if (GET_MODE_SIZE (t) == units)
  212.       val = t;
  213.  
  214.   return val;
  215. }
  216.  
  217. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  218.  
  219. tree
  220. build_int (v)
  221.      int v;
  222. {
  223.   register tree t;
  224.   /* Type-size nodes already made for small sizes.  */
  225.   static tree size_table[33];
  226.  
  227.   if (v < 33 && size_table[v] != 0)
  228.     return size_table[v];
  229.   if (v < 33)
  230.     {
  231.       int temp = allocation_temporary_p ();
  232.       /* Make this a permanent node.  */
  233.       if (temp)
  234.     end_temporary_allocation ();
  235.       t = build_int_2 (v, 0);
  236.       TREE_TYPE (t) = sizetype;
  237.       size_table[v] = t;
  238.       if (temp)
  239.     resume_temporary_allocation ();
  240.     }
  241.   else
  242.     {
  243.       t = build_int_2 (v, 0);
  244.       TREE_TYPE (t) = sizetype;
  245.     }
  246.   return t;
  247. }
  248.  
  249. /* Combine operands OP1 and OP2 with arithmetic operation OPC.
  250.    OPC is a tree code.  Data type is taken from `sizetype',
  251.    If the operands are constant, so is the result.  */
  252.  
  253. tree
  254. genop (opc, op1, op2)
  255.      enum tree_code opc;
  256.      tree op1, op2;
  257. {
  258.   /* Handle the special case of two integer constants faster.  */
  259.   if (TREE_CODE (op1) == INTEGER_CST && TREE_CODE (op2) == INTEGER_CST)
  260.     {
  261.       /* And some specific cases even faster than that.  */
  262.       if (opc == PLUS_EXPR
  263.       && TREE_INT_CST_LOW (op1) == 0
  264.       && TREE_INT_CST_HIGH (op1) == 0)
  265.     return op2;
  266.       if (opc == MINUS_EXPR
  267.       && TREE_INT_CST_LOW (op2) == 0
  268.       && TREE_INT_CST_HIGH (op2) == 0)
  269.     return op1;
  270.       if (opc == MULT_EXPR
  271.       && TREE_INT_CST_LOW (op1) == 1
  272.       && TREE_INT_CST_HIGH (op1) == 0)
  273.     return op2;
  274.       if (opc == CEIL_DIV_EXPR
  275.       && TREE_INT_CST_LOW (op1) == TREE_INT_CST_LOW (op2)
  276.       && TREE_INT_CST_HIGH (op1) == TREE_INT_CST_HIGH (op2))
  277.     return size_one_node;
  278.       /* Handle general case of two integer constants.  */
  279.       return combine (opc, op1, op2);
  280.     }
  281.  
  282.   if (op1 == error_mark_node || op2 == error_mark_node)
  283.     return error_mark_node;
  284.  
  285.   return fold (build (opc, sizetype, op1, op2));
  286. }
  287.  
  288. /* Convert a size which is SIZE when expressed in unit INUNITS
  289.    into the units OUTUNITS.  Rounds up if conversion is not exact.
  290.    If SIZE is constant, so is the result.  */
  291.  
  292. tree
  293. convert_units (size, inunits, outunits)
  294.    tree size;
  295.    register int inunits, outunits;
  296. {
  297.   register tree t;
  298.  
  299.   if (inunits == outunits)
  300.     return size;
  301.   /* Check for inunits divisible by outunits.
  302.      In that case, just multiply by their ratio.  */
  303.   if (0 == (inunits % outunits))
  304.     return genop (MULT_EXPR, size, build_int (inunits / outunits));
  305.   /* The inverse case.  */
  306.   if (0 == (outunits % inunits))
  307.     {
  308.       /* Discard anything in SIZE to round it up to a multiple
  309.      of a number N that divides our current divisor.  */
  310.       if (TREE_CODE (size) == MULT_EXPR
  311.       && TREE_CODE (TREE_OPERAND (size, 1)) == INTEGER_CST
  312.       && 0 == (outunits / inunits) % TREE_INT_CST_LOW (TREE_OPERAND (size, 1))
  313.       && TREE_CODE (TREE_OPERAND (size, 0)) == CEIL_DIV_EXPR
  314.       && tree_int_cst_equal (TREE_OPERAND (size, 1),
  315.                  TREE_OPERAND (TREE_OPERAND (size, 0), 1)))
  316.     size = TREE_OPERAND (TREE_OPERAND (size, 0), 0);
  317.       return genop (CEIL_DIV_EXPR, size, build_int (outunits / inunits));
  318.     }
  319.   /* The general case.  */
  320.   t = genop (MULT_EXPR, size,
  321.          build_int (inunits)); /* convert to bits */
  322.   return genop (CEIL_DIV_EXPR, t,
  323.         build_int (outunits)); /* then to outunits */
  324. }
  325.  
  326. /* Set the size, mode and alignment of a ..._DECL node.
  327.    TYPE_DECL does need this for C++.  It is up to language-specific
  328.    code to intialize the DECL_OFFSET of TYPE_DECL nodes.
  329.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  330.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  331.    Don't call layout_decl for them.
  332.  
  333.    KNOWN_ALIGN is the amount of alignment we can assume this
  334.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  335.    and depends on the previous fields.
  336.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  337.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  338.    the record will be aligned to suit.  */
  339.  
  340. void
  341. layout_decl (decl, known_align)
  342.      tree decl;
  343.      unsigned known_align;
  344. {
  345.   register tree type = TREE_TYPE (decl);
  346.   register enum tree_code code = TREE_CODE (decl);
  347.   int spec_size = DECL_SIZE_UNIT (decl);
  348.   int bitsize;
  349.  
  350.   if (code == CONST_DECL)
  351.     return;
  352.  
  353.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  354.       && code != FIELD_DECL && code != TYPE_DECL)
  355.     abort ();
  356.  
  357.   if (type == error_mark_node)
  358.     {
  359.       type = void_type_node;
  360.       spec_size = 0;
  361.     }
  362.   if (TYPE_SIZE_UNIT (type) == 0)
  363.     abort ();
  364.  
  365.   /* Usually the size and mode come from the data type without change.  */
  366.  
  367.   DECL_MODE (decl) = TYPE_MODE (type);
  368.   DECL_SIZE (decl) = TYPE_SIZE (type);
  369.   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
  370.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  371.  
  372.   if (code == FIELD_DECL && TREE_PACKED (decl))
  373.     {
  374.       /* This is a bit-field.  We don't know how to handle
  375.      them except for integers and enums, and front end should
  376.      never generate them otherwise.  */
  377.  
  378.       if (! (TREE_CODE (type) == INTEGER_TYPE
  379.          || TREE_CODE (type) == ENUMERAL_TYPE))
  380.     abort ();
  381.  
  382.       if (spec_size == 0)
  383.     abort ();
  384.  
  385.       /* Mode is "integer bit field".  */
  386.       DECL_MODE (decl) = BImode;
  387.       /* Size is specified number of bits.  */
  388.       DECL_SIZE (decl) = size_one_node;
  389.       DECL_SIZE_UNIT (decl) = spec_size;
  390.     }
  391.   /* Force alignment required for the data type.
  392.      But if the decl itself wants greater alignment, don't override that.  */
  393.   else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  394.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  395.  
  396.   if (DECL_SIZE (decl))
  397.     bitsize = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
  398.  
  399.   /* See if we can use a scalar mode such as QImode or SImode
  400.      in place of BLKmode or a packed byte mode.  */
  401.   /* Conditions are: a fixed size that is correct for another mode
  402.      and occupying a complete byte or bytes on proper boundary.  */
  403.   if ((DECL_MODE (decl) == BLKmode
  404.        || DECL_MODE (decl) == BImode)
  405.       /* Don't do this if DECL's type requires it to be BLKmode.  */
  406.       && TYPE_MODE (type) != BLKmode
  407.       && TYPE_SIZE (type) != 0
  408.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  409.     {
  410.       register enum machine_mode xmode = agg_mode (bitsize);
  411.  
  412.       if (xmode != BLKmode
  413.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  414.     {
  415.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  416.                    DECL_ALIGN (decl));
  417.       DECL_MODE (decl) = xmode;
  418.       DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
  419.       DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
  420.       bitsize = GET_MODE_BITSIZE (xmode);
  421.     }
  422.     }
  423.  
  424.   /* Don't let more than one word of an aggregate occupy one register,
  425.      since then the SUBREG used to access the high part would malfunction.
  426.      Check that the expected # of registers is big enough that they
  427.      seem to hold this variable with just a word per register.  */
  428.   if (DECL_SIZE (decl) != 0
  429.       && (TREE_CODE (type) == RECORD_TYPE
  430.       || TREE_CODE (type) == UNION_TYPE
  431.       || TREE_CODE (type) == ARRAY_TYPE))
  432.     {
  433.       /* This test is not exactly right, since we really want the minimum
  434.      number of regs in any class that can hold this mode.
  435.      But it does distinguish the machines we need to distinguish,
  436.      for now.  */
  437.       if (CLASS_MAX_NREGS (ALL_REGS, TYPE_MODE (type)) * BITS_PER_WORD
  438.       < bitsize)
  439.     TREE_ADDRESSABLE (decl) = 1;
  440.     }
  441.  
  442.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  443.   if (DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
  444.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  445. }
  446.  
  447. /* Lay out a RECORD_TYPE type (a C struct).
  448.    This means laying out the fields, determining their offsets,
  449.    and computing the overall size and required alignment of the record.
  450.    Note that if you set the TYPE_ALIGN before calling this
  451.    then the struct is aligned to at least that boundary.
  452.  
  453.    If the type has basetypes, you must call layout_basetypes
  454.    before calling this function.  */
  455.  
  456. static void
  457. layout_record (rec)
  458.      tree rec;
  459. {
  460.   register tree field;
  461. #ifdef STRUCTURE_SIZE_BOUNDARY
  462.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  463. #else
  464.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  465. #endif
  466.   /* These must be laid out *after* the record is.  */
  467.   tree pending_statics = NULL_TREE;
  468.   /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
  469.      where CONST_SIZE is an integer
  470.      and VAR_SIZE is a tree expression.
  471.      If VAR_SIZE is null, the size is just CONST_SIZE.
  472.      Naturally we try to avoid using VAR_SIZE.  */
  473.   register int const_size = 0;
  474.   register tree var_size = 0;
  475.   register int size_unit = BITS_PER_UNIT;
  476.  
  477. #if 0
  478.   /* If there are basetypes, the caller should already have
  479.      laid them out.  Leave space at the beginning for them.  */
  480.   if (TYPE_SIZE (rec) != 0)
  481.     {
  482.       if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  483.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  484.       else
  485.     var_size = TYPE_SIZE (rec);
  486.       size_unit = TYPE_SIZE_UNIT (rec);
  487.     }
  488. #endif /* 0 */
  489.  
  490.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  491.     {
  492.       register int desired_align;
  493.  
  494.       /* If FIELD is a VAR_DECL, then treat it like a separate variable,
  495.      not really like a structure field.
  496.      If it is a FUNCTION_DECL, it's a method.
  497.      In both cases, all we do is lay out the decl,
  498.      and we do it *after* the record is laid out.  */
  499.  
  500.       if (TREE_CODE (field) == VAR_DECL)
  501.     {
  502.       pending_statics = tree_cons (NULL, field, pending_statics);
  503.       continue;
  504.     }
  505.       /* Enumerators and enum types which are local to this class need not
  506.      be laid out.  */
  507.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  508.     continue;
  509.  
  510.       /* Lay out the field so we know what alignment it needs.
  511.      For KNOWN_ALIGN, pass the number of bits from start of record
  512.      or some divisor of it.  */
  513.  
  514.       layout_decl (field, var_size ? size_unit : const_size);
  515.       desired_align = DECL_ALIGN (field);
  516.  
  517.       /* Record must have at least as much alignment as any field.
  518.      Otherwise, the alignment of the field within the record
  519.      is meaningless.  */
  520.  
  521.       record_align = MAX (record_align, desired_align);
  522. #ifdef PCC_BITFIELD_TYPE_MATTERS
  523.       /* In PCC on Vax, Sony, etc., a bit field of declare type `int'
  524.      forces the entire structure to have `int' alignment.  */
  525.       if (DECL_NAME (field) != 0)
  526.     record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
  527. #endif
  528.  
  529.       /* Does this field automatically have alignment it needs
  530.      by virtue of the fields that precede it and the record's
  531.      own alignment?  */
  532.  
  533.       if (const_size % desired_align != 0
  534.       || (size_unit % desired_align != 0
  535.           && var_size))
  536.     {
  537.       /* No, we need to skip space before this field.
  538.          Bump the cumulative size to multiple of field alignment.  */
  539.  
  540.       if (var_size == 0
  541.           || size_unit % desired_align == 0)
  542.         const_size
  543.           = CEIL (const_size, desired_align) * desired_align;
  544.       else
  545.         {
  546.           var_size
  547.         = genop (PLUS_EXPR, var_size,
  548.              build_int (CEIL (const_size, size_unit)));
  549.           const_size = 0;
  550.           var_size = convert_units (var_size, size_unit, desired_align);
  551.           size_unit = desired_align;
  552.         }
  553.     }
  554.  
  555. #ifdef PCC_BITFIELD_TYPE_MATTERS
  556.       if (TREE_CODE (field) == FIELD_DECL
  557.       && TREE_TYPE (field) != error_mark_node)
  558.     {
  559.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  560.       register tree dsize = DECL_SIZE (field);
  561.       int field_size = TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  562.  
  563.       /* A bit field may not span the unit of alignment of its type.
  564.          Advance to next boundary if necessary.  */
  565.       if (const_size / type_align
  566.           != (const_size + field_size - 1) / type_align)
  567.         const_size = CEIL (const_size, type_align) * type_align;
  568.     }
  569. #endif
  570.  
  571.       /* Size so far becomes the offset of this field.  */
  572.  
  573.       DECL_OFFSET (field) = const_size;
  574.       DECL_VOFFSET (field) = var_size;
  575.       DECL_VOFFSET_UNIT (field) = size_unit;
  576.  
  577.       /* If this field is an anonymous union,
  578.      give each union-member the same offset as the union has.  */
  579.  
  580.       if (DECL_NAME (field) == 0
  581.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  582.     {
  583.       tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  584.       for (; uelt; uelt = TREE_CHAIN (uelt))
  585.         {
  586.           DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  587.           DECL_OFFSET (uelt) = DECL_OFFSET (field);
  588.           DECL_VOFFSET (uelt) = DECL_VOFFSET (field);
  589.           DECL_VOFFSET_UNIT (uelt) = DECL_VOFFSET_UNIT (field);
  590.         }
  591.     }
  592.  
  593.       /* Now add size of this field to the size of the record.  */
  594.  
  595.       {
  596.         register tree dsize = DECL_SIZE (field);
  597.  
  598.     if (TREE_LITERAL (dsize))
  599.       const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  600.     else if (var_size == 0)
  601.       {
  602.         var_size = dsize;
  603.         size_unit = DECL_SIZE_UNIT (field);
  604.       }
  605.     else
  606.       {
  607.         register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
  608.         var_size
  609.           = genop (PLUS_EXPR,
  610.                convert_units (var_size, size_unit, tunits),
  611.                convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
  612.       }
  613.       }
  614.     }
  615.  
  616.   /* Work out the total size and alignment of the record
  617.      as one expression and store in the record type.
  618.      Round it up to a multiple of the record's alignment.  */
  619.  
  620.   if (var_size == 0)
  621.     TYPE_SIZE (rec)
  622.       = build_int (CEIL (CEIL (const_size, record_align) * record_align,
  623.              size_unit));
  624.   else
  625.     {
  626.       if (const_size)
  627.     var_size
  628.       = genop (PLUS_EXPR, var_size,
  629.            build_int (CEIL (const_size, size_unit)));
  630.       TYPE_SIZE (rec)
  631.     = convert_units (var_size,
  632.              size_unit,
  633.              record_align);
  634.       size_unit = record_align;
  635.     }
  636.  
  637.   TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
  638.                    BITS_PER_UNIT);
  639.   TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  640.   TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
  641.  
  642.   /* Lay out any static members.  This is done now
  643.      because their type may use the record's type.  */
  644.  
  645.   for (field = pending_statics; field; field = TREE_CHAIN (field))
  646.     layout_decl (TREE_VALUE (field), 0);
  647. }
  648.  
  649. /* Lay out a UNION_TYPE type.
  650.    Lay out all the fields, set their offsets to zero,
  651.    and compute the size and alignment of the union (maximum of any field).
  652.    Note that if you set the TYPE_ALIGN before calling this
  653.    then the union align is aligned to at least that boundary.  */
  654.  
  655. static void
  656. layout_union (rec)
  657.      tree rec;
  658. {
  659.   register tree field;
  660. #ifdef STRUCTURE_SIZE_BOUNDARY
  661.   int union_align = STRUCTURE_SIZE_BOUNDARY;
  662. #else
  663.   int union_align = BITS_PER_UNIT;
  664. #endif
  665.  
  666.   /* The size of the union, based on the fields scanned so far,
  667.      is max (CONST_SIZE, VAR_SIZE).
  668.      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
  669.   register int const_size = 0;
  670.   register tree var_size = 0;
  671.  
  672.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  673.     {
  674. #if 0 /* This should be in a language-specific file
  675.      since it needs to use language-specific terminology.  */
  676.       if (TREE_STATIC (field))
  677.     {
  678.       error_with_decl (field, "field `%s' declared static in union");
  679.       TREE_STATIC (field) = 0;
  680.     }
  681. #endif
  682.  
  683.       /* Ignore enumerators and enum types local to the union.  */
  684.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  685.     continue;
  686.  
  687.       layout_decl (field, 0);
  688.       DECL_OFFSET (field) = 0;
  689.       DECL_VOFFSET (field) = 0;
  690.       DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
  691.  
  692.       /* Union must be at least as aligned as any field requires.  */
  693.  
  694.       union_align = MAX (union_align, DECL_ALIGN (field));
  695.  
  696. #ifdef PCC_BITFIELD_TYPE_MATTERS
  697.       /* On the m88000, a bit field of declare type `int'
  698.      forces the entire union to have `int' alignment.  */
  699.       union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
  700. #endif
  701.  
  702.       /* Set union_size to max (decl_size, union_size).
  703.      There are more and less general ways to do this.
  704.      Use only CONST_SIZE unless forced to use VAR_SIZE.  */
  705.  
  706.       if (TREE_LITERAL (DECL_SIZE (field)))
  707.     const_size = MAX (const_size,
  708.               TREE_INT_CST_LOW (DECL_SIZE (field))
  709.               * DECL_SIZE_UNIT (field));
  710.       else if (var_size == 0)
  711.     var_size = convert_units (DECL_SIZE (field),
  712.                   DECL_SIZE_UNIT (field),
  713.                   BITS_PER_UNIT);
  714.       else
  715.     var_size = genop (MAX_EXPR,
  716.               convert_units (DECL_SIZE (field),
  717.                      DECL_SIZE_UNIT (field),
  718.                      BITS_PER_UNIT),
  719.               var_size);
  720.     }
  721.  
  722.   /* Determine the ultimate size of the union (in bytes).  */
  723.   if (NULL == var_size)
  724.     TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
  725.   else if (const_size == 0)
  726.     TYPE_SIZE (rec) = var_size;
  727.   else
  728.     TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
  729.                  build_int (CEIL (const_size, BITS_PER_UNIT)));
  730.  
  731.   /* Determine the desired alignment.  */
  732.   union_align = MIN (BIGGEST_ALIGNMENT, union_align);
  733.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
  734.  
  735.   /* Round the size up to be a multiple of the required alignment */
  736.   TYPE_SIZE (rec)
  737.     = convert_units (TYPE_SIZE (rec), BITS_PER_UNIT, TYPE_ALIGN (rec));
  738.   TYPE_SIZE_UNIT (rec) = TYPE_ALIGN (rec);
  739. }
  740.  
  741. /* Calculate the mode, size, and alignment for TYPE.
  742.    For an array type, calculate the element separation as well.
  743.    Record TYPE on the chain of permanent or temporary types
  744.    so that dbxout will find out about it.
  745.  
  746.    TYPE_SIZE of a type is nonzero if the type has been laid out already.
  747.    layout_type does nothing on such a type.
  748.  
  749.    If the type is incomplete, its TYPE_SIZE remains zero.  */
  750.  
  751. void
  752. layout_type (type)
  753.      tree type;
  754. {
  755.   int old;
  756.   int temporary = 0;
  757.  
  758.   if (type == 0)
  759.     abort ();
  760.  
  761.   /* Do nothing if type has been laid out before.  */
  762.   if (TYPE_SIZE (type))
  763.     return;
  764.  
  765.   /* Make sure all nodes we allocate are not momentary;
  766.      they must last past the current statement.  */
  767.   old  = suspend_momentary ();
  768.   if (TREE_PERMANENT (type) && allocation_temporary_p ())
  769.     {
  770.       temporary = 1;
  771.       end_temporary_allocation ();
  772.     }
  773.  
  774.   chain_type (type);
  775.  
  776.   switch (TREE_CODE (type))
  777.     {
  778.     case LANG_TYPE:
  779.       /* This kind of type is the responsibility
  780.      of the languge-specific code.  */
  781.       abort ();
  782.  
  783.     case VOID_TYPE:
  784.       TYPE_SIZE (type) = size_zero_node;
  785.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  786.       TYPE_ALIGN (type) = 1;
  787.       TYPE_MODE (type) = VOIDmode;
  788.       break;
  789.  
  790.     case INTEGER_TYPE:
  791.     case ENUMERAL_TYPE:
  792.       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  793.     TREE_UNSIGNED (type) = 1;
  794.  
  795.       /* What follows is like agg_mode except that it ignores
  796.      MAX_FIXED_MODE_SIZE.  That applies only to structures.  */
  797.       {
  798.     enum machine_mode mode, t;
  799.  
  800.     /* Get the last mode which has this size.  */
  801.     mode = BLKmode;
  802.     for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  803.          t = (enum machine_mode) ((int) t + 1))
  804.       if (GET_MODE_BITSIZE (t) == TYPE_PRECISION (type))
  805.         mode = t;
  806.  
  807.     TYPE_MODE (type) = mode;
  808.       }
  809. #if defined( DSP56000 )
  810.       /* this is lame, but in L mode, we still need to let the compiler think
  811.      that DImode has 48 bits of precision, while also making it think that
  812.      it occupies one storage location. */
  813.       if ( LONG_TYPE_SIZE == TYPE_PRECISION ( type ))
  814.       {
  815.       TYPE_MODE ( type ) = DImode;
  816.       TYPE_SIZE ( type ) = build_int (( 'l' == memory_model ) ? 1 : 2 );
  817.       }
  818.       else
  819.       {
  820.       TYPE_SIZE ( type ) = 
  821.           build_int ( GET_MODE_SIZE ( TYPE_MODE ( type )));
  822.       }
  823. #else
  824.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  825. #endif
  826.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  827.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  828.       break;
  829.  
  830.     case REAL_TYPE:
  831.       {
  832. #if defined( DSP96000 ) 
  833.       /* 1) on the 96k SP (floats) and SEP (double) numbers use the
  834.          same amount of memory under the l memory model, (so the
  835.          TYPE_PRECISION test isn't always valid. 
  836.          2) on the 56k, we make everything SFmode, as DFmode and SFmode
  837.          are exactly the same thing. this occurs naturally with the
  838.          FSF code below. */
  839.       if ( type == float_type_node )
  840.       {
  841.           TYPE_MODE (type) = SFmode;
  842.       }
  843.       else
  844.       {
  845.           TYPE_MODE (type) = DFmode;
  846.       }
  847. #else
  848.     register int prec = TYPE_PRECISION (type);
  849.     if (prec <= GET_MODE_BITSIZE (SFmode))
  850.       TYPE_MODE (type) = SFmode;
  851.     else if (prec <= GET_MODE_BITSIZE (DFmode))
  852.       TYPE_MODE (type) = DFmode;
  853.     else if (prec <= GET_MODE_BITSIZE (TFmode))
  854.       TYPE_MODE (type) = TFmode;
  855.     else
  856.       abort ();
  857. #endif
  858.       }
  859.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  860.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  861.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  862.       break;
  863.  
  864.     case POINTER_TYPE:
  865.     case REFERENCE_TYPE:
  866.       TYPE_MODE (type) = Pmode;
  867. #if defined( DSP56000 )
  868.       /* a pointer must occupy an entire mem unit. */
  869.       TYPE_SIZE (type) = build_int ( 1 );
  870. #else
  871.       TYPE_SIZE (type) = build_int (POINTER_SIZE / BITS_PER_UNIT);
  872. #endif
  873.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  874.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  875.       TREE_UNSIGNED (type) = 1;
  876.       TYPE_PRECISION (type) = POINTER_SIZE;
  877.       break;
  878.  
  879.     case ARRAY_TYPE:
  880.       {
  881.     register tree index = TYPE_DOMAIN (type);
  882.     register tree length;
  883.     register tree element = TREE_TYPE (type);
  884.  
  885. /*     layout_type (element);  */
  886.     build_pointer_type (element);
  887.  
  888.     if (index == 0)
  889.       length = 0;
  890.     else
  891.       length = genop (PLUS_EXPR, size_one_node,
  892.               genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  893.                  TYPE_MIN_VALUE (index)));
  894.  
  895.     if (TREE_PACKED (type))
  896.       abort ();  /* ??? Not written yet since not needed for C.  */
  897.  
  898.     TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
  899.     if (length && TYPE_SIZE (element))
  900.       TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
  901.     TYPE_SEP (type) = TYPE_SIZE (element);
  902.     TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
  903.     TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  904.     TYPE_MODE (type) = BLKmode;
  905.     if (TYPE_SIZE (type) != 0
  906.         && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  907.         /* BLKmode elements force BLKmode aggregate;
  908.            else extract/store fields may lose.  */
  909.         && TYPE_MODE (TREE_TYPE (type)) != BLKmode
  910. #ifdef STRICT_ALIGNMENT
  911.         && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  912.         || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  913.                      * TYPE_SIZE_UNIT (type)))
  914. #endif
  915.         )
  916.       {
  917.         TYPE_MODE (type)
  918.           = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  919.               * TYPE_SIZE_UNIT (type));
  920.       }
  921.     break;
  922.       }
  923.  
  924.     case RECORD_TYPE:
  925.       layout_record (type);
  926.       TYPE_MODE (type) = BLKmode;
  927.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  928.       /* If structure's known alignment is less than
  929.          what the scalar mode would need, and it matters,
  930.          then stick with BLKmode.  */
  931. #ifdef STRICT_ALIGNMENT
  932.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  933.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  934.                        * TYPE_SIZE_UNIT (type)))
  935. #endif
  936.       )
  937.     {
  938.       tree field;
  939.       /* A record which has any BLKmode members must itself be BLKmode;
  940.          it can't go in a register.  */
  941.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  942.         {
  943.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  944.         goto record_lose;
  945.  
  946.           /* Must be BLKmode if any field crosses a word boundary,
  947.          since extract_bit_field can't handle that in registers.  */
  948.           if (DECL_OFFSET (field) / BITS_PER_WORD
  949.           != ((TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  950.                + DECL_OFFSET (field) - 1)
  951.               / BITS_PER_WORD))
  952.         goto record_lose;
  953.         }
  954.       
  955.       TYPE_MODE (type)
  956.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  957.             * TYPE_SIZE_UNIT (type));
  958.     record_lose: ;
  959.     }
  960.       break;
  961.  
  962.     case UNION_TYPE:
  963.       layout_union (type);
  964.       TYPE_MODE (type) = BLKmode;
  965.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  966.       /* If structure's known alignment is less than
  967.          what the scalar mode would need, and it matters,
  968.          then stick with BLKmode.  */
  969. #ifdef STRICT_ALIGNMENT
  970.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  971.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  972.                        * TYPE_SIZE_UNIT (type)))
  973. #endif
  974.       )
  975.     {
  976.       tree field;
  977.       /* A union which has any BLKmode members must itself be BLKmode;
  978.          it can't go in a register.  */
  979.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  980.         if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  981.           goto union_lose;
  982.  
  983.       TYPE_MODE (type)
  984.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  985.             * TYPE_SIZE_UNIT (type));
  986.     union_lose: ;
  987.     }
  988.       break;
  989.  
  990.     case FUNCTION_TYPE:
  991.     case METHOD_TYPE:
  992.       TYPE_MODE (type) = EPmode;
  993.       TYPE_SIZE (type) = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  994.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  995.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  996.       break;
  997.  
  998.     default:
  999.       abort ();
  1000.     } /* end switch */
  1001.  
  1002.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  1003.   if (TYPE_SIZE (type) != 0 && ! TREE_LITERAL (TYPE_SIZE (type)))
  1004.     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
  1005.  
  1006.   /* Also layout any other variants of the type.  */
  1007.   if (TYPE_NEXT_VARIANT (type)
  1008.       || type != TYPE_MAIN_VARIANT (type))
  1009.     {
  1010.       tree variant;
  1011.       /* Record layout info of this variant.  */
  1012.       tree size = TYPE_SIZE (type);
  1013.       int size_unit = TYPE_SIZE_UNIT (type);
  1014.       int align = TYPE_ALIGN (type);
  1015.       enum machine_mode mode = TYPE_MODE (type);
  1016.  
  1017.       /* Copy it into all variants.  */
  1018.       for (variant = TYPE_MAIN_VARIANT (type);
  1019.        variant;
  1020.        variant = TYPE_NEXT_VARIANT (variant))
  1021.     {
  1022.       TYPE_SIZE (variant) = size;
  1023.       TYPE_SIZE_UNIT (variant) = size_unit;
  1024.       TYPE_ALIGN (variant) = align;
  1025.       TYPE_MODE (variant) = mode;
  1026.     }
  1027.     }
  1028.     
  1029.   if (temporary)
  1030.     resume_temporary_allocation ();
  1031.   resume_momentary (old);
  1032. }
  1033.  
  1034. /* Create and return a type for signed integers of PRECISION bits.  */
  1035.  
  1036. tree
  1037. make_signed_type (precision)
  1038.      int precision;
  1039. {
  1040.   register tree type = make_node (INTEGER_TYPE);
  1041.  
  1042.   TYPE_PRECISION (type) = precision;
  1043.  
  1044.   /* Create the extreme values based on the number of bits.  */
  1045.  
  1046.   TYPE_MIN_VALUE (type)
  1047.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
  1048.            (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
  1049.               ? precision-HOST_BITS_PER_INT-1
  1050.               : 0));
  1051.   TYPE_MAX_VALUE (type)
  1052.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
  1053.            (precision-HOST_BITS_PER_INT-1 > 0
  1054.             ? (1<<(precision-HOST_BITS_PER_INT-1))-1
  1055.             : 0));
  1056.  
  1057.   /* Give this type's extreme values this type as their type.  */
  1058.  
  1059.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1060.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1061.  
  1062.   /* The first type made with this or `make_unsigned_type'
  1063.      is the type for size values.  */
  1064.  
  1065.   if (sizetype == 0)
  1066.     sizetype = type;
  1067.  
  1068.   /* Lay out the type: set its alignment, size, etc.  */
  1069.  
  1070.   layout_type (type);
  1071.  
  1072.   return type;
  1073. }
  1074.  
  1075. /* Create and return a type for unsigned integers of PRECISION bits.  */
  1076.  
  1077. tree
  1078. make_unsigned_type (precision)
  1079.      int precision;
  1080. {
  1081.   register tree type = make_node (INTEGER_TYPE);
  1082.  
  1083.   TYPE_PRECISION (type) = precision;
  1084.  
  1085.   /* The first type made with this or `make_unsigned_type'
  1086.      is the type for size values.  */
  1087.  
  1088.   if (sizetype == 0)
  1089.     sizetype = type;
  1090.  
  1091.   fixup_unsigned_type (type);
  1092.   return type;
  1093. }
  1094.  
  1095. /* Set the extreme values of TYPE based on its precision in bits,
  1096.    the lay it out.  This is used both in `make_unsigned_type'
  1097.    and for enumeral types.  */
  1098.  
  1099. void
  1100. fixup_unsigned_type (type)
  1101.      tree type;
  1102. {
  1103.   register int precision = TYPE_PRECISION (type);
  1104.  
  1105.   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  1106.   TYPE_MAX_VALUE (type)
  1107.     = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
  1108.            precision-HOST_BITS_PER_INT > 0
  1109.            ? ((unsigned) ~0
  1110.               >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
  1111.            : 0);
  1112.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1113.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1114.  
  1115.   /* Lay out the type: set its alignment, size, etc.  */
  1116.  
  1117.   layout_type (type);
  1118. }
  1119.